#!/bin/zsh

# ======================================================================== alias for quickstart

# folder system
alias cdesktop="cd ${HOME}/Desktop"
alias fdesktop="open ${HOME}/Desktop"

# folder $WORKPATH (follow main)
alias fapplications="open ${WORKPATH}/applications"
alias fdata="open ${WORKPATH}/data"
alias fdownload="open ${WORKPATH}/download"
alias ffield="open ${WORKPATH}/field"
alias ftemporary="open ${WORKPATH}/temporary"
alias fwork="open ${WORKPATH}/work"
alias ftrash="open ${WORKPATH}/trash"

# folder field (follow main)
alias fstudy="open ${WORKPATH}/field/gitee-elvis/Study"
alias ftcode="open ${WORKPATH}/field/gitee-elvis/TestCode"
alias fccode="open ${WORKPATH}/field/gitee-elvis/CaseCode"
alias fdpool="open ${WORKPATH}/field/gitee-elvis/DataPool"
alias fdocuments="open ${WORKPATH}/field/documents"
alias fdevelop="open ${WORKPATH}/field/develop"
alias fnote="open ${WORKPATH}/field/note"

# ======================================================================== boot items

# sshd
if ! ps | grep sshd > /dev/null; then
    /bin/sshd 1>/dev/null 2>&1
    # no host key and generate it (default /etc/ssh/ssh_host_*)
    if [ $? -eq 1 ]; then
        ssh-keygen -A && /bin/sshd 1>/dev/null 2>&1
        log_note 'generated host key'
    fi
fi

# generate /etc/passwd
if [ ! -f /etc/passwd ]; then
    # NOTE: call this command should first set `db_shell: /usr/bin/zsh` in /etc/nsswitch.conf
    mkpasswd -l > /etc/passwd
    log_note 'generated passwd'
fi

# ======================================================================== functions

# open(file/dir/pattern, ...), open the specified file or director
# open(exe -- arg), open the exe with arguments
function open () {
    if [ $# -eq 0 ]; then
        log_error 'please specify a file or directory'
        return 255
    fi
    local p
    # check whether the arguments contains `--`
    if [ $# -gt 2 ] && [ "$2" = '--' ]; then
        # open(exe -- arg) mode
        local exec
        p=$(realpath "$1")
        # check whether the exe file exist
        if [ -f "$p" ]; then
            exec=$p
        elif type "$1" 1>/dev/null 2>&1; then
            exec=$1
        else
            log_error "executable file ($1) not exist"
            return 255
        fi
        # execute
        for arg in "${@:3}"; do
            if [ -e "$arg" ]; then
                start '' "$exec" "$arg"
            else
                log_warning "$arg not exist"
            fi
        done
    else
        # open(file/dir/pattern, ...) mode
        for f in "$@"; do
            p=$(realpath "$f")
            if [ -e "$p" ]; then
                start '' "$p"
            elif type "$f" 1>/dev/null 2>&1; then
                start '' "$f"
            else
                log_warning "($f) not exist"
            fi
        done
    fi
}

# opend(file/dir/pattern, ...), open the specified file or director which on the desktop
# opend(exe -- arg), open the exe with arguments
function opend () {
    if [ $# -eq 0 ]; then
        log_error 'please specify a file or directory'
        return 255
    fi
    local p
    # check whether the arguments contains `--`
    if [ $# -gt 2 ] && [ "$2" = '--' ]; then
        # opend(exe -- arg) mode
        p=$(realpath "${HOME}/Desktop/$1")
        # check whether the exe file exist
        if [ -f "$p" ]; then
            for arg in "${@:3}"; do
                if [ -e "$arg" ]; then
                    start '' "$p" "$(realpath "$arg")"
                else
                    log_warning "$arg not exist"
                fi
            done
        else
            log_error "executable file ($1) not exist"
            return 255
        fi
    else
        # opend(file/dir/pattern, ...) mode
        local tmp
        # don't split fullname by space (set whatever a char)
        tmp=$IFS && IFS='?'
        for f in "$@"; do
            p=$(realpath "${HOME}/Desktop/$f")
            IFS=$'\n'
            # note: can't use "" to package $p here, because it will made the $p to a string with space split
            for i in $p; do
                if [ -e "$i" ]; then
                    start '' "$i"
                else
                    log_warning "($f) not exist in desktop"
                fi
            done
        done
        IFS=$tmp
    fi
}
function __opend_complation () {
    # get the content of the previous input
    local prev=$words[CURRENT-1]
    case $prev in
        '--')
            _files
            ;;
        *)
            # get the items in desktop
            local dt_items=("${HOME}/Desktop"/*)
            _values 'desktop folder' "${dt_items[@]##*/}"   # `##*/` to get the basename
            ;;
    esac
}
compdef __opend_complation opend
